package mytest.ssw.www.api;

import android.util.Log;

import java.util.List;

import mytest.ssw.www.mylibrary.DeptNode;

/**
 * Created by hasee on 2016/5/2.
 */
public class DeptNodeFoundationImpl implements DeptNodeFoundation {
    SqliteTools<DeptNode> sqlT;
    String fileDir;
    public DeptNodeFoundationImpl(String fileDir){
        this.fileDir=fileDir;
    }

    @Override
    public String initDB(){
        sqlT = new SqliteTools<DeptNode>(fileDir);
        String sql = "create table IF NOT EXISTS  TB_DEPT( "+
                "   _id  integer primary key autoincrement," +
                "   id  varchar(50) NOT NULL," +
                "   num  varchar(50) DEFAULT NULL, " +
                "   name  varchar(20) DEFAULT NULL, " +
                "   fullName  varchar(100) DEFAULT NULL, " +
                "   pNum  varchar(50) DEFAULT NULL, " +
                "   level  int(1) DEFAULT NULL, " +
                "   leaf  int(1) DEFAULT NULL, " +
                "   orderNum  varchar(15) DEFAULT NULL, " +
                "   valid  int(1) DEFAULT NULL " +
                ")";
        sqlT.initDB(sql);
        return "";
    }
    //根据pNum从本地获取DeptNode
    public List<DeptNode> getLocalByPNum(String pNum){
        //initDB();
        sqlT = new SqliteTools<DeptNode>(fileDir);
     String sql="select * from TB_DEPT where pNum = '" + pNum + "' order by orderNum";
        Log.i("BMSUCE", sql);
        return sqlT.queryBySql(DeptNode.class,sql);
    }

    public List<DeptNode> getAllLocal(){
        sqlT = new SqliteTools<DeptNode>(fileDir);
        String sql="select * from TB_DEPT";
        Log.i("BMSUCE", sql);
        return sqlT.queryBySql(DeptNode.class,sql);
    }

    public List<DeptNode> getAllLocalByLevel(int level){
        Log.d("getAllLocalByLevel",""+fileDir);
        sqlT = new SqliteTools<DeptNode>(fileDir);
        String sql="select * from TB_DEPT where level >="+level+" order by orderNum ";
        Log.i("BMSUCE", sql);
        return sqlT.queryBySql(DeptNode.class,sql);
    }
}
